#-------------------------------------------------------------------------------
# Name:        Debug
# Purpose:
#
# Author:      Pablo
#
# Created:     29/05/2012
# Copyright:   (c) Monxcleyr Productions 2012
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

from ..gui import label
import pygame

class Debug():
  """ Handles displaying dynamic debug information.  """

  def __init__(self, enabled, surface, leftpos, rightpos, lifetime, version):
    self.enabled = enabled
    self.pos1 = leftpos
    self.pos2 = rightpos
    self.font = pygame.font.Font('resources/fonts/system.ttf', 12)
    self.surface = surface
    self.label1 = label.Label(self.surface, '1', self.pos1, 'left', self.font, (255, 255, 0))
    self.label2 = label.Label(self.surface, '1', (self.surface.get_width() - self.pos2[0], self.pos2[1]), 'right', self.font, (255, 255, 0))
    self.entries = []
    self.lifetime = int(lifetime * 70)
    self.version = version
    self.right = ''
    self.argv = ''
    self.oldright = pygame.Rect(0,0,0,0)
    self.oldleft = pygame.Rect(0,0,0,0)


  def setEnabled(self, enabled):
    self.enabled = enabled

  def setArgv(self, argv):
    self.argv = ''
    for arg in argv:
      self.argv += arg + ' '

  def add(self, entry):
    self.entries.append([entry, self.lifetime])

  def setRight(self, entry):
    self.right = entry

  def resetWidth(self, width):
    self.label2.pos = (width - self.pos2[0], self.pos2[1])

  def draw(self, screen, fps, averagefps, lowestfps, highestfps, runtime, time, mode, modes):

    """ Draw debug screen.
    Debug.draw(screen, fps, averagefps, lowestfps, highestfps, runtime, time) -> rect list.
    Returns a list containing two rects that contain affected pixels. """

    self.surface.fill((0,0,0,0))
    # left and right rects that contain everything being drawn, so later we can clear them
    leftrectlist = []
    self.leftrect = pygame.Rect(0,0,0,0)
    self.rightrect = pygame.Rect(0,0,0,0)
    if self.enabled == True:
      # Right Debug area
##      self.label2.setText(self.right)
##      self.label2.screen = screen
##      self.rightrect = self.label2.draw()

      # left Debug area
      self.label1.font_col = (255,255,0)
      self.label1.screen = screen
      self.label1.pos = (self.pos1)
      self.label1.setText('Engine Version: ' + self.version)
      temprect = self.label1.draw()
      leftrectlist.append(temprect)
      self.label1.setText('FPS: ' + str(fps) + ' avgFPS: ' + str(averagefps) + ' lowFPS: ' \
        + str(lowestfps) + ' highFPS: ' + str(highestfps))
      self.label1.pos = (self.label1.pos[0], self.label1.pos[1] + self.label1.height)
      temprect = self.label1.draw()
      leftrectlist.append(temprect)
      self.label1.setText('Run time: ' + str(runtime) + ' FPS (millisec): ' + str(time))
      self.label1.pos = (self.label1.pos[0], self.label1.pos[1] + self.label1.height)
      temprect = self.label1.draw()
      leftrectlist.append(temprect)
      self.label1.setText('Mode: ' + str(mode) + ' ' + str(modes[mode]))
      self.label1.pos = (self.label1.pos[0], self.label1.pos[1] + self.label1.height)
      temprect = self.label1.draw()
      leftrectlist.append(temprect)



      if self.argv != '':
        self.label1.pos = (self.label1.pos[0], self.label1.pos[1] + self.label1.height)
        self.label1.setText('Arg: ' + self.argv)
        temprect = self.label1.draw()
        leftrectlist.append(temprect)

      # Updating list of debug logs
      self.label1.font_col = (255,255,128)
      for entry in reversed(self.entries):
        if entry[1] != 0:
          self.label1.pos = (self.label1.pos[0], self.label1.pos[1] + self.label1.height)
          self.label1.setText(entry[0])
          temprect = self.label1.draw()
          leftrectlist.append(temprect)
          entry[1] -= 1
        else:
          self.entries.remove(entry)


      # create themerged rect that will hold everything changed from teh left debug stuff
      self.leftrect = leftrectlist[0]
      for i in range(1, len(leftrectlist)):
        self.leftrect.union_ip(leftrectlist[i])

    return [self.rightrect, self.leftrect, self.oldleft, self.oldright]

  def clear(self, screen, bg):

    screen.blit(bg, self.leftrect, self.leftrect)
    screen.blit(bg, self.rightrect, self.rightrect)
    screen.blit(bg, self.oldleft, self.oldleft)
    screen.blit(bg, self.oldright, self.oldright)
    self.oldleft = self.leftrect
    self.oldright = self.rightrect

